#!/bin/bash
# Program:
#	Configure Ubuntu
#
#	Conditions:
#	   Ubuntu, Kubuntu, Lubuntu, Xubuntu, and other OSes based on ubuntu
#
# History:
#     2014/04/10 Potter  Version 1.1

# set -e

# Root
# if [ "$UID" -ne "0" ]
if [ "$(id -u)" -ne "0" ]
then
    echo "Sorry, this program must be running by Root."
    exit
fi

logfile=/var/log/ConfigureUbuntu.log
logDir

if [ -d $HOME/ConfigureUbuntu ]
then
	rm -rf $HOME/ConfigureUbuntu
fi
mkdir $HOME/ConfigureUbuntu && cd $HOME/ConfigureUbuntu

# Log File
touch $logfile

# backup sources.list
if [ ! -f /etc/apt/sources.list.old ]
then
    cp /etc/apt/sources.list /etc/apt/sources.list.old
fi
apt-get update

download_log() {
    for argv in "$@"
    do
	apt-get install -y $argv && echo "Succeed in installing --> $argv" >> $logfile || echo "ERROR: Failed to install $argv" >> $logfile
    done
}

#echo "deb http://debian.ustc.edu.cn/ubuntu/ raring main multiverse restricted universe
#deb http://debian.ustc.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
#deb http://debian.ustc.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
#deb http://debian.ustc.edu.cn/ubuntu/ raring-security main multiverse restricted universe
#deb http://debian.ustc.edu.cn/ubuntu/ raring-updates main multiverse restricted universe
#deb-src http://debian.ustc.edu.cn/ubuntu/ raring main multiverse restricted universe
#deb-src http://debian.ustc.edu.cn/ubuntu/ raring-backports main multiverse restricted universe
#deb-src http://debian.ustc.edu.cn/ubuntu/ raring-proposed main multiverse restricted universe
#deb-src http://debian.ustc.edu.cn/ubuntu/ raring-security main multiverse restricted universe
#deb-src http://debian.ustc.edu.cn/ubuntu/ raring-updates main multiverse restricted universe" > /etc/apt/sources.list

# apt-get update || echo "error: apt-get update failed." >> $logfile

# Bradcom 802.11 Linux STA 无线网卡驱动
# apt-get install -y bcmwl-kernel-source && \
# apt-get install -y broadcom-sta-common && \
# apt-get install -y broadcom-sta-common && \
# apt-get install -y broadcom-sta-source && \
# Broad 43xx 固件提取工具
# apt-get install -y b43-fwcutter && \
# apt-get install -y firmware-b43-installer && \
# 激活无线网卡
# modprobe -r b43 ssb && \
# modprobe b43  || echo "error: 无线网卡安装失败." >> $logfile

# 调教Nvidia驱动
# http://wiki.ubuntu.org.cn/NVIDIA
# https://github.com/Bumblebee-Project/Bumblebee
# https://launchpad.net/~bumblebee/+archive/stable
#add-apt-repository ppa:bumblebee/stable && \
#apt-get update && \
#apt-get install -y bumblebee bumblebee-nvidia  
#add-apt-repository ppa:ubuntu-x-swat/x-updates && \
#apt-get update && \
#apt-get install -y nvidia-current nvidia-settings && \
# 从事GPU开发的可能还需要装上cuda和openCL库的支持
#apt-get install -y nvidia-current-dev || echo "error: 安装Nvidia drivers failed." >> $logfile
# "Need reboot
# "The Follow to See how Nvidia Run
# sudo optirun nvidia-settings -c :8
# "To run program using Nvidia
# sudo optirun + program_name
#
#
# add following to /etc/apt/sourcelist
# 12.04 and Me (kernel 3.8)
#	deb http://ppa.launchpad.net/bumblebee/stable/ubuntu precise main 
#	deb-src http://ppa.launchpad.net/bumblebee/stable/ubuntu precise main 
# 
# 13.10:
#	deb http://ppa.launchpad.net/bumblebee/stable/ubuntu saucy main 
#	deb-src http://ppa.launchpad.net/bumblebee/stable/ubuntu saucy main 
#
# 14.04:
#       deb http://ppa.launchpad.net/bumblebee/stable/ubuntu trusty main 
#	deb-src http://ppa.launchpad.net/bumblebee/stable/ubuntu trusty main 
#
# 14.04
download_log bumblebee

# 安装需要的程序
# Program:
# vim and tools
download_log vim tig

# git
download_log git gitk subversion 

# c/c++
download_log gcc g++
# IDE
download_log codeblocks geany

# java

# python
download_log python python-setuptools python-virtualenv python-pip python-django-south ipython

# django-1.5.5
pip install Django==1.5.5 && echo "Succeed in installing --> Django==1.5.5" >> $logfile
if [ "$?" != "0" ]
then
    # git clone https://github.com/django/django.git
    wget -O Django-1.5.5.tar.gz https://www.djangoproject.com/m/releases/1.5/Django-1.5.5.tar.gz && \
    tar -zxvf Django-1.5.5.tar.gz && \
    cd Django-1.5.5 && \
    python setup.py install && \
    cd .. && \
    echo "Succeed in installing --> Django" >> $logfile || echo "ERROR: Failed to install Django-1.5.5" >> $logfile
fi

# django-debug-toolbar
# version 0.8.5
git clone https://gitcafe.com/Potter/django-debug-toolbar django-debug && \
# Django-debug-toolbar Latest Version 1.2
# git clone https://github.com/django-debug-toolbar/django-debug-toolbar django-debug && \
cd django-debug && \
python setup.py install && \
cd .. || echo "ERROR: Failed to install django-debug-toolbar" >> $logfile

# Database
download sqlite3 mysql-server mysql-client python-mysqldb

# Life
download_log apcalc smplayer flashplugin-installer

# system
download_log ssh filezilla virtualbox convmv tree 
# VBoxGuestAdditions
# 	http://download.virtualbox.org/virtualbox

# FTP
download_log vsftpd

# dhcpcd
download_log dhcpcd
# Small caching DNS proxy and DHCP/TFTP server
download_log dnsmasq

# input method
download_log fcitx fcitx-googlepinyin

# fonts
download_log ttf-wqy-zenhei

# OpenGL
download_log freeglut3*

# python-setuptools --> easy_install
# 利用easy_install 安装virtualenv
# easy_install virtualenv

# 安装Brackets.io , html, js
# http://brackets.io
wget -O Brackets.deb https://github.com/adobe/brackets/releases/download/sprint-38/Brackets.Sprint.38.64-bit.deb && \
dpkg -i Brackets.deb || echo "error: failed to install brackets.deb" >> $logfile

# 安装 chrome
download_log google-chrome-stable && \
download_log libxss1 && \
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
dpkg -i google-chrome-stable_current_amd64.deb || \
echo "error: failed to install google-chrome" >> $logfile

# Install Goagent
# https://wiki.archlinux.org/index.php/GoAgent_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)
git clone https://github.com/goagent/goagent goagent

# njit-client
# apt-get install -y libpcap-dev libssl-dev pkg-config
# apt-get install -y automake autoconf
# git clone https://github.com/linzsummer/njit8021xclient_for_swufe && \
# cd njit* && \
# chmod +x configure && \
# ./configure && \
# make && make install && \
# cd .. || \
# rm -rf njit8021xclie* || 
# echo "error: failed to install njit-client." >> $logfile
# rm -rf njit8021x*
#
########
# Straight Download binary file: njit-clinet
#
wget -O /usr/bin/njit-client https://gitcafe.com/Potter/Softwares/raw/master/Njit-client/njit-client && \
chmod +x /usr/bin/njit-client && echo "Succeed in installing --> njit-client" >> $logfile

# 好用的音乐播放器 iceplayer
mkdir iceplayer && cd iceplayer
download_log morituri gstreamer0.10-plugins-ugly w3m
# gstreamer0.10-ffmpeg
# http://packages.ubuntu.com/quantal/amd64/gstreamer0.10-ffmpeg/download
echo "deb http://cz.archive.ubuntu.com/ubuntu quantal main universe" >> /etc/apt/sources.list && \
apt-get update && \
apt-get install -y gstreamer0.10-ffmpeg
# apt-get install -y libavcodec53>=4:0.8-1~
apt-get -f install && \
# if ! $?
# then
#   apt-get -f install
# fi
wget -O iceplayer.deb https://iceplayer.googlecode.com/files/iceplayer-amd64-4.0.5-20110520.deb && \
dpkg -i iceplayer.deb
# 解决依赖
# 针对ubuntu14.04 kernel 3.11.x 缺少libnotify.so.1
download_log python-notify
ln -s /usr/lib/x86_64-linux-gnu/libnotify.so.4 /usr/lib/x86_64-linux-gnu/libnotify.so.1
#
cd ..

# Download Goagent
git clone http://git.oschina.net/sunnypotter/goagent.git /usr/share/goagent && ln -s /usr/share/goagent/local/proxy.sh /usr/bin/goagent && ln -s /usr/share/goagent/server/uploader.sh /usr/bin/goagent_upload_data && echo "Succeeding in installing --> goagent" >> $logfile || echo "ERROR: failed to install --> goagent" >> $logfile

# $HOME/.vimrc
wget -O $HOME/.vimrc http://git.oschina.net/sunnypotter/Old/raw/master/notes/myvimrc.notes

# Log Report:
cat $logfile
echo "More detail, please look at $logfile"
# rm -rf $logfile*

echo "Reboot 10 seconds later..."
sleep 10

# reboot 
read -p "Are you sure to reboot ?(Y/N)" answer
if [ "$answer" == "Y" ] || [ "$answer" == "y" ]; then
	reboot
fi

exit 
